package com.wwz.leetcode;

import org.junit.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LeetCode17 {

    public List<String> letterCombinations(String digits) {

        List<String> resList = new ArrayList<String>();
        if (digits.length() == 0) {
            return resList;
        }
        Map<Character, String> phoneMap = new HashMap<Character, String>() {{
            put('2', "abc");
            put('3', "def");
            put('4', "ghi");
            put('5', "jkl");
            put('6', "mno");
            put('7', "pqrs");
            put('8', "tuv");
            put('9', "wxyz");
        }};
        backtrack(resList, phoneMap, digits, 0, new StringBuilder());
        return resList;
    }

    public void backtrack(List<String> resList, Map<Character, String> phoneMap, String digits, int index, StringBuilder sb) {
        if (index == digits.length()) {
            resList.add(sb.toString());
        } else {
            char digit = digits.charAt(index);
            // 找到字母
            String letters = phoneMap.get(digit);
            int lettersCount = letters.length();
            for (int i = 0; i < lettersCount; i++) {
                sb.append(letters.charAt(i));
                backtrack(resList, phoneMap, digits, index + 1, sb);
                // 删除匹配好的  为了往后接着枚举
                sb.deleteCharAt(index);
            }
        }
    }

    @Test
    public void test() {
        System.out.println(letterCombinations("23"));
    }
}
